package com.nettest.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.nettest.action.BaseJsonAction;
import com.nettest.bean.system.DictCriteriaVo;
import com.nettest.entity.system.Dictionary;
import com.nettest.pojo.Page;
import com.nettest.service.event.DictionaryUpdatedEvent;
import com.nettest.service.system.DictionaryService;
import com.nettest.util.StringUtils;
@Results({
	@Result(name = "success", location = "/system/dictManage.jsp")})
@Action("dictManage")
@Namespace("/system/dict")
public class DictionaryManageAction extends BaseJsonAction implements ApplicationContextAware {

    private static final long serialVersionUID = 456254024151703135L;
    private String operate;
    private Dictionary dict;
    private List<Dictionary> dictList;
    private DictCriteriaVo criteria;
    @Autowired
    private DictionaryService dictionaryService;
    
    private ApplicationContext applicationContext;
    
    @Action(value = "edit",results={@Result(name = "success", location = "/system/dictEdit.jsp")})
    public String edit() throws Exception {
    	if (!StringUtils.isBlank(dict == null ? "" : String.valueOf(dict.getId()))) {
        	dict = dictionaryService.get(dict.getId());
        }
        return SUCCESS;
    }
    @Action(value = "save",results={@Result(name = "success", location = "/system/dictEdit.jsp")})
    public String save() throws Exception {
        if (dict != null) {
        	Long id = dict.getId();
        	if(id == null) {
        		dictionaryService.save(dict);
        	} else {
        		Dictionary dict1 = dictionaryService.get(dict.getId());
        		dict1.setCategory(dict.getCategory());
        		dict1.setOptionText(dict.getOptionText());
        		dict1.setOptionValue(dict.getOptionValue());
        		dict1.setSort(dict.getSort());
				dictionaryService.save(dict1);
        	}
            applicationContext.publishEvent(new DictionaryUpdatedEvent(this));
        }
        return SUCCESS;
    }
    
    @Action(value = "delete",results={@Result(name = "success", location = "/system/dictEdit.jsp")})
    public String delete() throws Exception {
        if (dict != null) {
            dictionaryService.delete(dict.getId());
            applicationContext.publishEvent(new DictionaryUpdatedEvent(this));
        }
        return SUCCESS;
    }
    @SuppressWarnings("unchecked")
	@Action(value = "list",results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "criteria,dictionaryService,dict,operate"}) })
    public String list()throws Exception {
		Page page = new Page();
		page.setStart(this.getFirstResult());
		page.setLimit(this.getMaxResults());
		page.setSortField(this.getSort());
		page.setDescOrAsc(this.getOrder());
		page.setObjCondition(criteria);
		page = dictionaryService.findByPageDictionary(page);
		if(page != null) {
			dictList = (List<Dictionary>) page.getRoot();
			setTotal(page.getTotalProperty());
		}
		return SUCCESS;
	}
    
    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

	public Dictionary getDict() {
		return dict;
	}

	public void setDict(Dictionary dict) {
		this.dict = dict;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public DictCriteriaVo getCriteria() {
		return criteria;
	}

	public void setCriteria(DictCriteriaVo criteria) {
		this.criteria = criteria;
	}
	@JSON(name="rows")
	public List<Dictionary> getDictList() {
		return dictList;
	}

	public void setDictList(List<Dictionary> dictList) {
		this.dictList = dictList;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}
